<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 工具栏 -->
      <PageTool :show-before="false">
        <template v-slot:after>
          <el-button type="primary" @click="addPerm('0',1)">添加权限</el-button>
        </template>
      </PageTool>
      <!-- 表格 -->
      <el-card>
        <el-table row-key="id" :data="permList">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type === 1" type="text" @click="addPerm(row.id,2)">添加</el-button>
              <el-button type="text" @click="editPerm(row.id)">编辑</el-button>
              <el-button type="text" @click="delPerm(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增权限 -->
      <el-dialog
        :title="formData.id?'编辑权限':'新增权限'"
        :visible="showDialog"
        @close="closeFn"
      >
        <el-form ref="form" :rules="rules" :model="formData" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="'1'"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template type="flex" justify="center">
          <el-row>
            <el-button size="small" @click="closeFn">取 消</el-button>
            <el-button type="primary" size="small" @click="submit">确 定</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { allPermission, addPermission, delPermission, editPermission, getPersonalDetail } from '@/api/permission'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      permList: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: null,
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取权限列表
    this.getPermList()
  },
  methods: {
    // 编辑按钮
    async editPerm(id) {
      this.showDialog = true
      // 数据回显
      const res = await getPersonalDetail(id)
      this.formData = res.data.data
    },
    // 删除按钮
    delPerm(id) {
      this.$confirm('是否确定删除？', '提示', {
        type: 'warning'
      }).then(async() => {
        // 调用接口 实现删除
        await delPermission(id)
        // 更新列表
        this.getPermList()
      }).catch(err => {
        console.log('点击了取消按钮：', err)
      })
    },
    // 确定按钮
    submit() {
      this.$refs.form.validate(async bool => {
        if (bool) {
          if (this.formData.id) {
            await editPermission(this.formData)
          } else {
            // 通过校验调用接口
            await addPermission(this.formData)
          }
          // 关闭弹窗
          this.closeFn()
          // 刷新接口
          this.getPermList()
        }
      })
    },
    // 关闭弹窗
    closeFn() {
      this.showDialog = false
      // 清空表单数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: null,
        pid: ''
      }
      // 重置校验规则
      this.$refs.form.resetFields()
    },
    addPerm(pid, type) {
      // 保存pid和type
      this.formData.pid = pid
      this.formData.type = type
      // 开启弹窗
      this.showDialog = true
    },
    async getPermList() {
      const res = await allPermission()
      // 保存数据，转换成树形结构
      this.permList = listToTree(res.data.data, '0')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin-top: 20px;
}
</style>
